package com.yunxi.service.dubboService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdOrderMonthVersion;
import com.yunxi.otd.pojo.OtdOrderMonthVersionChangeStateVO;
import com.yunxi.otd.pojo.OtdOrderMonthVersionQueryVO;
import com.yunxi.otd.pojo.OtdOrderMonthVersionVO;
import com.yunxi.otd.service.OtdOrderMonthVersionDubboService;
import com.yunxi.service.localService.otd.OtdOrderMonthVersionService;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 * 月排产需求订单版本管理 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-07-15
 */
public class OtdOrderMonthVersionDubboServiceImpl implements OtdOrderMonthVersionDubboService {

    @Autowired
    @Qualifier(value = "otdOrderMonthVersionServiceImpl")
    OtdOrderMonthVersionService otdOrderMonthVersionService;


    @Override
    public OtdOrderMonthVersionVO getById(String id) {
        return convertVO(otdOrderMonthVersionService.getById(id));
    }

    @Override
    public ApiResult changeState(OtdOrderMonthVersionChangeStateVO changeStateVO) {
        OtdOrderMonthVersion monthVersion = new OtdOrderMonthVersion();
        BeanUtils.copyProperties(changeStateVO, monthVersion);
        otdOrderMonthVersionService.updateById(monthVersion);
        return ResponseUtil.ok();
    }

    @Override
    public ApiResult removeByIds(List<String> ids) {
        return otdOrderMonthVersionService.removeByIds(ids);
    }

    @Override
    public PageInfo findPageByVO(OtdOrderMonthVersionQueryVO queryVO, Integer pageNum,
        Integer pageSize) {
        return otdOrderMonthVersionService.pageByVo(queryVO, pageNum, pageSize);
    }

    @Override
    public List listByVo(OtdOrderMonthVersionVO otdOrderMonthVersionVO) {
        return otdOrderMonthVersionService.listByVo(otdOrderMonthVersionVO);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult setDefault(String id) {
        // 取消原有默认版本
        OtdOrderMonthVersion cancelMonthVersion = new OtdOrderMonthVersion();
        cancelMonthVersion.setIsDefault(0);
        LambdaQueryWrapper<OtdOrderMonthVersion> cancelWrapper = new LambdaQueryWrapper<>();
        cancelWrapper.eq(OtdOrderMonthVersion::getIsDefault,1);
        otdOrderMonthVersionService.update(cancelMonthVersion,cancelWrapper);

        //根据ID设置位默认版本
        OtdOrderMonthVersion setMonthVersion = new OtdOrderMonthVersion();
        setMonthVersion.setIsDefault(1);
        LambdaQueryWrapper<OtdOrderMonthVersion> setWrapper = new LambdaQueryWrapper<>();
        setWrapper.eq(OtdOrderMonthVersion::getId,id);
        if(!otdOrderMonthVersionService.update(setMonthVersion,setWrapper)){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseUtil.fail(-1,"设置失败请稍后重试！");
        }
        return ResponseUtil.ok();
    }

    /**
     * BEAN to VO
     *
     * @param bean BEAN
     * @return VO
     */
    private OtdOrderMonthVersionVO convertVO(OtdOrderMonthVersion bean) {
        if (bean == null) {
            return null;
        }
        OtdOrderMonthVersionVO vo = new OtdOrderMonthVersionVO();
        BeanUtils.copyProperties(bean, vo);
        return vo;
    }
}
